package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.RecommendUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/20 20:22
 * @Version 1.0
 */

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //今日佳人
    @Override
    public RecommendUser findTodayBest(Long toUserId) {

        //构建条件
        Query query = new Query(Criteria.where("toUserId").is(toUserId))
                        .with(Sort.by(Sort.Order.desc("score")))
                        .skip(0).limit(1);

        //查询一个
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        return recommendUser;


    }

    // 推荐用户
@Override
public PageBeanVo findRecommendUserByPage(Long toUserId, Integer pageNum, Integer pageSize) {
    // 1.构建条件
    Integer index = (pageNum - 1) * pageSize + 1;
    Query query = new Query(Criteria.where("toUserId").is(toUserId))
        .with(Sort.by(Sort.Order.desc("score")))
        .skip(index).limit(pageSize);
    // 2.查询list
    List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
    // 3.返回并封装分页对象
    return new PageBeanVo(pageNum, pageSize, 0l, recommendUserList);
}
    //推荐用户详情
    @Override
    public RecommendUser findPersonalInfo(Long toUserId, Long jiarenId) {

        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
                        .and("userId").is(jiarenId)
        );

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        return recommendUser;


    }
}
